package com.yylending.las.personal.loan;

import android.text.TextUtils;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * P:我的借款列表
 * Created by ying_he on 2017/6/5.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class LoanListPresenter extends Presenter<LoanListModel, LoanListPresenter.View> {

	private static final String TAG = LoanListPresenter.class.getSimpleName();
	private static final String NO_RETURN_DATA = "-1";

	private View mView;

	public static class Generator implements PresenterFactory<LoanListPresenter> {
		@Override
		public LoanListPresenter create() {
			return new LoanListPresenter();
		}
	}

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new LoanListModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public interface View extends IView {
		void refreshSuccess(ArrayList<LoanListModel.Loan.LoanDetail> list);
		void obtainLoanListFailed(String tips);
	}

	void obtainLoanList() {
		execute(new EmptyEntity(), FUNCTIONAL_CODE, false);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void obtainLoanListResult(LoanListModel.LoanListResult result) {
		if (BaseResultEntity.isResponseSuccess(result)) {
			if (result.getData() != null && !TextUtils.equals(NO_RETURN_DATA, result.getData().loanStatus) && result.getData().detail.size() > 0) {
				mView.refreshSuccess(result.getData().detail);
			} else {
				mView.refreshSuccess(new ArrayList<LoanListModel.Loan.LoanDetail>());
			}
		} else {
			mView.obtainLoanListFailed(result.getRetMsg());
		}
	}
}
